package com.example.demo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.framework.ResultData;
import com.example.demo.framework.base.BaseController;
import com.example.demo.model.Notice;
import com.example.demo.model.ReadRecord;
import com.example.demo.model.UserInfo;
import com.example.demo.repository.ItemRepository;
import com.example.demo.repository.NoticeRepository;
import com.example.demo.repository.ReadRecordRepository;
import com.example.demo.repository.UserRepository;

/**
 * 公告通知
 * @author jc
 *
 */
@RestController
@RequestMapping("notice")
public class NoticeController extends BaseController  {

	@Autowired
	NoticeRepository noticeRepository;
	
	@Autowired
	ItemRepository itemRepository;
	
	@Autowired
	ReadRecordRepository readRecordRepository;
	
	@Autowired
	UserRepository userRepository;
	
	
	/**
	 * 公告列表
	 * @param pageable
	 * @return
	 */
	@RequestMapping("findNotice")
	public Object findNotice(@RequestParam(value = "pageNo", defaultValue = "0") int pageNo,Notice notice) {
		Map<String, Object> data = new HashMap<>();
		
		Page<Notice> list = noticeRepository.pageCriteria(notice,new QPageRequest(pageNo(pageNo), PAGESIZE));
		if (getUser().getUserType()==1) {
			for (int i = 0; i < list.getNumberOfElements(); i++) {
				ReadRecord record=readRecordRepository.findByNID(getUser().getUserID(),list.getContent().get(i).getnID());
				list.getContent().get(i).setStatus(record.getStatus());
			}
		}
		data.put("list", list);
		return new ResultData(data);
	}
	
	/**
	 * 发布公告
	 * @param notice
	 * @return
	 */
	@Transactional
	@RequestMapping("addNotice")
	public Object addNotice(Notice notice) {
		Map<String, Object> data = new HashMap<>();
		notice.setUpdateTime(System.currentTimeMillis());
		noticeRepository.save(notice);
		List<UserInfo> list=userRepository.findByUserList();
		for (int i = 0; i < list.size(); i++) {
			ReadRecord readRecord=new ReadRecord();
			readRecord.setnID(notice.getnID());
			readRecord.setuID(list.get(i).getUserID());
			readRecord.setStatus(0);
			readRecordRepository.save(readRecord);
		}
		data.put("notice", notice);
		return new ResultData(data);
	}
	
	/**
	 * 代理商-已读公告
	 * @param notice
	 * @return
	 */
	@Transactional
	@RequestMapping("editAgentNotice")
	public Object editAgentNotice(Notice notice) {
		Map<String, Object> data = new HashMap<>();
		ReadRecord record=readRecordRepository.findByNID(new Long((long)3),notice.getnID());
		if (record.getStatus()==0) {
			record.setStatus(1);
			record.setReadTime(System.currentTimeMillis());
			readRecordRepository.save(record);
		}
		data.put("record", record);
		return new ResultData(data);
	}
	
	
}
